package cn.hl.controller;

import cn.hl.sentinel.OrderBlockHandler;
import cn.hl.sentinel.OrderFallback;
import cn.hl.domain.User;
import cn.hl.feign.UserServer;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private UserServer userServer;

    @GetMapping("/getUserById/{id}")
    @SentinelResource(value = "order-getUserById",
            blockHandler = "getUserById",
            blockHandlerClass = OrderBlockHandler.class,
            fallback = "getById",
            fallbackClass = OrderFallback.class)
    public User getUserById(@PathVariable("id") Long id) {
//        int i = 1/0;
        return userServer.getUserById(id);
    }

/*    public User getUserById(@PathVariable("id") Long id, BlockException e) {
        return new User(-1L,"限流","请求处理不过来了！");
    }*/

    /*public User getById(@PathVariable("id") Long id, Throwable e) {
        return new User(-1L, "熔断", "服务不可用，请稍后重试！");
    }*/
}
